/* Step 1: Import & Prepare Shift-Share Dataset */
clear all

import delimited "$input\shift_share_data_set.csv", clear

/* Convert date variables to Stata formats */
gen time = date(date, "YMD")
gen qdate = qofd(time)  // Convert to quarterly format
format time %td
format qdate %tq

/* Convert loan start date to Stata format */
gen startoutsideloan = date(datestart, "YMD")
gen qstartoutsideloan = qofd(startoutsideloan)
format startoutsideloan %td
format qstartoutsideloan %tq

/* Sort data */
sort siren qdate

/* Generate treatment indicators for firms that received loans */
gen p2pqtrcapturing = qdate >= qstartoutsideloan if p2p == 1
gen outsideloanqtrcapturing = qdate >= qstartoutsideloan if p2p == 0
gen p2pqtrcapturingstrict = qdate > qstartoutsideloan if p2p == 1
gen outsideloanqtrcapturings = qdate > qstartoutsideloan if p2p == 0

/* Compute time difference from loan start */
gen deltaquarter = qdate - qstartoutsideloan

/* Define treatment window */
gen deltaqdummy = deltaquarter >= -4 & deltaquarter <= 8
replace deltaqdummy = . if deltaquarter == .

/* Step 2: Industry Classification Adjustments */
tab industry p2p

/* Aggregate industries */
replace industry = "public" if inlist(industry, "P", "Q", "R", "S")  // Public sector
replace industry = "DE" if inlist(industry, "D", "E")  // Merge D and E
replace industry = "FL" if inlist(industry, "F", "L")  // Merge F and L

/* Drop irrelevant industries */
drop if inlist(industry, "A", "K", "O", "B", "DE", "0000Z")

/* Generate grouped industry variable */
egen industry10 = group(industry)

/* Standardize ratings */
replace rating = "3A" if rating == "3++"
replace rating = "3B" if rating == "3+"
replace rating = "3C" if rating == "3"
replace rating = "4A" if rating == "4+"
replace rating = "4B" if rating == "4"
replace rating = "5A" if rating == "5+"
replace rating = "5B" if rating == "5"

encode rating, gen(ratinge)
replace ratinge = ratinge - 1
replace ratinge = 13 if ratinge == 0

/* Set time-series structure */
tsset siren qdate, quarterly

/* Keep only observations within the required date range */
keep if qdate < qofd(date("20200101","YMD"))
keep if qdate > qofd(date("20131231","YMD"))

/* Save Shift-Share Dataset */
tempfile dataset1
save `dataset1'

/* Step 3: Open Log File */
capture log close
log using "$logs\compliers.log", replace

/* Step 4: Import Firm-Level Credit Data */
use "$output\ifp_scrapped_aggregated_industries_modified.dta", clear

/* Keep only quarterly observations */
keep if inlist(datemonth, 3, 6, 9, 12)

format mdate %tm
gen qdate = qofd(dofm(mdate))
format qdate %tq

/* Compute log-transformed loan amounts */
gen lloan = log(ltloan + bm + bi)
gen lttotal = ltloan + bm + bi
gen lloanfilled = lloan
gen lloan_total = log(stloan + ltloan + bm + bi + oc)

/* Sort data */
sort siren qdate

/* Define loan start date */
gen startoutsideloan = date(outsideloandate, "YMD")
gen qstartoutsideloan = qofd(startoutsideloan)
gen deltaquarter = qdate - qstartoutsideloan

/* Keep only firms in the t-test sample */
merge m:1 siren using "$output\ifp_ttest_siren_list.dta", keepusing(siren)
keep if _merge == 3
drop _merge

/* Step 5: Generate Credit Line Indicator */
preserve
keep if delta < 0
collapse (sum) co, by(siren)
gen codummy = co > 0
drop co
save "$output\data_co_dummy.dta", replace
restore

/* Encode industry and rating categories */
egen ratingint = group(rating)
replace ratingint = ratingint - 1
replace ratingint = 13 if ratingint == 0

/* Merge with Credit Line Indicator */
merge m:1 siren using "$output\data_co_dummy.dta"

/* Assign Variable Labels */
label var ageatevent "Age"
label var r1filled "Working capital"
label var r3filled "EBIT"
label var r5filled "Sales"
label var r12filled "Investment"
label var r13filled "Debt"
label var logassets_filled "Total Assets"
label var logemployees "Employment"
label var r_collateral_filled "Tangible assets"
label var rdcostsfilled "R&D costs filled"
label var ratingint "Rating"
label var codummy "$\mathbbm{1}$(Credit\ line)"

/* Define Global Control Variables */
global control `"r1filled r3filled r5filled r12filled r13filled logassets_filled logemployees r_collateral_filled ratingint"'

/* Step 6: Generate Fixed Firm Characteristics */
preserve
keep if deltaquarter == -4
keep siren r_collateral_filled ageatevent logassets_filled rating
rename (r_collateral_filled ageatevent logassets_filled rating) (r_collateral_filled_pre ageatevent_pre logassets_filled_pre rating_pre)
save "$output\characteristics.dta", replace
restore

/* Merge Fixed Characteristics */
merge m:1 siren using "$output\characteristics.dta", nogen keep(1 3)

/* Step 7: Keep Relevant Variables */
keep qdate siren lloan lloan_total $control outsideloan loantype lttotal forinvestment_complete r5filled unrated r_collateral_filled_pre ageatevent_pre logassets_filled_pre

/* Merge with Shift-Share Dataset */
merge 1:1 siren qdate using `dataset1', nogen 

/* Filter on dates available in Shift-Share dataset */
keep if qdate >= tq(2014q1)

/* Save Dataset */
tempfile dataset2
save `dataset2'

/* Step 8: Add Weights from PSM Sample */
use "$output\matched_sample_nn.dta", clear

/* Filter on required date range */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Keep quarterly observations */
keep if inlist(datemonth, 3, 6, 9, 12)

format mdate %tm
gen qdate = qofd(dofm(mdate))
format qdate %tq
sort siren qdate

/* Merge PSM Weights */
keep qdate siren weight year
merge 1:1 siren qdate using `dataset2', nogen 

/* Step 9: Generate Fixed Effects Variables */
egen ind_qtr = group(industry10 qdate)
egen size_qtr = group(size qdate)
egen reg_qtr = group(region qdate)
egen rat_qtr = group(ratinge qdate)

/* Step 10: Define Post Variables */
replace outsideloanqtrcapturing = 0 if outsideloanqtrcapturing == .
replace p2pqtrcapturing = 0 if p2pqtrcapturing == .
gen post = p2pqtrcapturing + outsideloanqtrcapturing

replace outsideloanqtrcapturings = 0 if outsideloanqtrcapturings == .
replace p2pqtrcapturingstrict = 0 if p2pqtrcapturingstrict == .
gen poststrict = p2pqtrcapturingstrict + outsideloanqtrcapturings

/* Step 11: Create Missing Data Indicators */
foreach var in $control {
    gen `var'_0 = `var'
    replace `var'_0 = 0 if `var' == .
    gen dum_`var' = (`var' == .)
}

global control_0 `"*_0 dum_*"'

	
/**********************************
* Main Table: Shift-Share Analysis
**********************************/

tsset siren qdate, quarterly
sort siren qdate

/**********************************
* Create Credit Score Variables
**********************************/

/* Define a credit score variable using secured loans */
gen scoredeltap2p = secured02

/* Keep this credit score fixed at the quarter when a P2P loan was granted */
replace scoredeltap2p = . if p2pqtrcapturing == 0
replace scoredeltap2p = . if !(L.p2pqtrcapturing == 0 & p2pqtrcapturing == 1)

/* Carry forward the score within firms */
by siren: carryforward scoredeltap2p, replace
by siren: replace scoredeltap2p = scoredeltap2p[_N]

/* Repeat the same process for conventional loans */
gen scoredeltacontran = secured02
replace scoredeltacontran = . if outsideloanqtrcapturing == 0
replace scoredeltacontran = . if !(L.outsideloanqtrcapturing == 0 & outsideloanqtrcapturing == 1)
by siren: carryforward scoredeltacontran, replace
by siren: replace scoredeltacontran = scoredeltacontran[_N]

/* Combine both scores, assigning P2P scores to P2P firms and conventional scores to others */
gen scoredeltaboth = scoredeltacontran if p2p == 0
replace scoredeltaboth = scoredeltap2p if p2p == 1

/* Interaction term: Secured ratio × Post-treatment period */
gen scoredeltapost = scoredeltaboth * post

/**********************************
* Control Variable: Loan Volume 
**********************************/

/* Create shift-share control of credit supply volume */
gen volloanp2pv = logloan02
replace volloanp2pv = . if p2pqtrcapturing == 0
replace volloanp2pv = . if !(L.p2pqtrcapturing == 0 & p2pqtrcapturing == 1)
by siren: carryforward volloanp2pv, replace
by siren: replace volloanp2pv = volloanp2pv[_N]

/* Same process for conventional loans */
gen volloancontranv = logloan02
replace volloancontranv = . if outsideloanqtrcapturing == 0
replace volloancontranv = . if !(L.outsideloanqtrcapturing == 0 & outsideloanqtrcapturing == 1)
by siren: carryforward volloancontranv, replace
by siren: replace volloancontranv = volloancontranv[_N]

/* Assign loan volumes according to firm type */
gen volloanbothv = volloancontranv if p2p == 0
replace volloanbothv = volloanp2pv if p2p == 1

/* Interaction term: Credit supply × Post-treatment period */
gen volloanv = volloanbothv * post

/**********************************
* Define Sample for Analysis
**********************************/

/* Use instrumental variable regression with fixed effects */
eststo second: ivreghdfe lloan (p2pqtrcapturing = scoredeltapost) post volloanv $control_0 if deltaqdummy, cl(siren) a(qdate siren)

/* Store sample indicator */
gen sample = e(sample)

/**********************************
* Create Firm Characteristic Groups
**********************************/

/* Define firm characteristics based on median values within industries */
foreach x in r_collateral_filled_pre ageatevent_pre logassets_filled_pre r5filled {
    bys industry10: egen median_`x' = median(`x') if sample
    gen high_`x' = `x' > median_`x' & `x' <. if sample
    replace high_`x' = . if `x' == .
}

/* Define high-risk firms based on rating */
gen high_risk = rating_pre >= 6 | rating_pre == 1 

/* Define firm characteristic categories */
gen small = !high_logassets
gen low_tangible = !high_r_collat
gen young = !high_age

/**********************************
* Interaction Terms for Regression
**********************************/

/* Interaction of firm characteristics with secured loan ratio and post-treatment */
gen small_score = scoredeltapost * small
gen young_score = scoredeltapost * young
gen low_tangible_score = scoredeltapost * low_tangible
gen high_risk_score = scoredeltapost * high_risk

/* Interaction of firm characteristics with post-treatment only */
gen small_post = post * small
gen young_post = post * young
gen low_tangible_post = post * low_tangible
gen high_risk_post = post * high_risk

/**********************************
* Variable Labels for Output Table
**********************************/

label var scoredeltapost "Secured ratio $\times$ Post"
label var post "Post"
label var p2pqtrcapturing "FinTech $\times$ Post"
label var volloanv "Credit supply  $\times$ Post"
label var small_score "Small $\times$ Secured ratio $\times$ Post"
label var young_score "Young $\times$ Secured ratio $\times$ Post"
label var low_tangible_score "Low tangibility $\times$ Secured ratio $\times$ Post"
label var high_risk_score "High risk $\times$ Secured ratio $\times$ Post"
label var small_post "Small $\times$ Post"
label var young_post "Young $\times$ Post"
label var low_tangible_post "Low tangibility $\times$ Post"
label var high_risk_post "High risk $\times$ Post"

/**********************************
* Regression Analysis 
**********************************/

/* Regression for small firms */
eststo small: reghdfe p2pqtrcapturing scoredeltapost post small_score small_post volloanv $control_0 if sample, cl(siren) a(qdate siren)
estadd local qdate "Y", replace
estadd local siren "Y", replace
estadd local control "Y", replace

/* Regression for firms with low tangible assets */
eststo low_tangible: reghdfe p2pqtrcapturing scoredeltapost post low_tangible_score low_tangible_post volloanv $control_0 if sample, cl(siren) a(qdate siren)
estadd local qdate "Y", replace
estadd local siren "Y", replace
estadd local control "Y", replace

/* Regression for high-risk firms */
eststo high_risk: reghdfe p2pqtrcapturing scoredeltapost post high_risk_score high_risk_post volloanv $control_0 if sample, cl(siren) a(qdate siren)
estadd local qdate "Y", replace
estadd local siren "Y", replace
estadd local control "Y", replace

/* Regression for young firms */
eststo young: reghdfe p2pqtrcapturing scoredeltapost post young_score young_post volloanv $control_0 if sample, cl(siren) a(qdate siren)
estadd local qdate "Y", replace
estadd local siren "Y", replace
estadd local control "Y", replace

/**********************************
* Export Table I2
**********************************/

esttab small low_tangible high_risk young using "$tables\shift-share_compliers.tex", replace ///
    prehead(" &\multicolumn{4}{c}{FinTech} \\ \cmidrule(lr){2-5}") ///
    star(* 0.10 ** 0.05 *** 0.01) nomtitles fragment gap booktabs label nonote noobs eqlabels(,none) b(%9.3f) se(%9.3f) ///
    stats(qdate siren control N r2, labels("Quarter FE" "Firm FE" "Controls" ///
    "\midrule Observations" "R-sq") ///
    layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ///
    "\multicolumn{1}{c}{@}") ///
    fmt(%9.0fc %9.0fc %9.0fc %9.0fc %9.3fc)) ///
    keep(scoredeltapost post small_score low_tangible_score high_risk_score young_score) ///
    order(scoredeltapost post small_score low_tangible_score high_risk_score young_score)

log close

	

